﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;

namespace Besture.UI.Admin.Product
{
    public partial class UserProductSet : BasePage
    {
        

        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
           if(!IsPostBack)
           {
             bindUID();
			 bindPID();
			
            if(Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                //编辑编号
                hidEditID.Value = Request.QueryString["id"];                   
                if(!Fun.IsNumber(hidEditID.Value))
                {
                    RedirectErrorUrl("编号错误");
                }               
                InitData(Convert.ToInt32(hidEditID.Value));               
            }           
           }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }
        
        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            UserProductDac dac = new UserProductDac();
            UserProductModel model = dac.GetModel(id);
            ddlUID.SelectedValue = model.UID.ToString();
			ddlPID.SelectedValue = model.PID.ToString();
			txtBuyDate.Text = model.BuyDate!=null?model.BuyDate.Value.ToString("yyyy-MM-dd"):"";
            txtEndDate.Text = model.EndDate != null ? model.EndDate.Value.ToString("yyyy-MM-dd") : "";
			rbtnState.SelectedValue = model.State.ToString();
			
        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            UserProductDac dac = new UserProductDac();
            UserProductModel model = null;
            string editID = hidEditID.Value;
            //添加
            if(string.IsNullOrEmpty(editID))
            {
                model = new UserProductModel();
                model.UID = ddlUID.SelectedValue!=""?Convert.ToInt32(ddlUID.SelectedValue):0;
				model.PID = ddlPID.SelectedValue!=""?Convert.ToInt32(ddlPID.SelectedValue):0;
				model.BuyDate =txtBuyDate.Text.Trim()!=""?Convert.ToDateTime(txtBuyDate.Text.Trim()):DateTime.Now;
                model.State = rbtnState.SelectedValue != "" ? Convert.ToInt32(rbtnState.SelectedValue) : 0;
                if (txtEndDate.Text.Trim() != "")
                {
                    model.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                }
                model.IsDeleted = 0;
                model.CreateTime = DateTime.Now;

                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.UID = ddlUID.SelectedValue!=""?Convert.ToInt32( ddlUID.SelectedValue):0;
				model.PID = ddlPID.SelectedValue!=""?Convert.ToInt32( ddlPID.SelectedValue):0;
                model.BuyDate = txtBuyDate.Text.Trim() != "" ? Convert.ToDateTime(txtBuyDate.Text.Trim()) : DateTime.Now;
                if (txtEndDate.Text.Trim() != "")
                {
                    model.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                }
				model.State = rbtnState.SelectedValue!=""?Convert.ToInt32( rbtnState.SelectedValue):0;
                model.IsDeleted = 0;

                dac.Update(model);
            }
            Fun.Alert("保存成功", "UserProductList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("UserProductList.aspx");
        }

        #endregion

       /// <summary>
		///绑定用户名
		/// </summary>
		public void bindUID()
		{
				UserDac dac = new UserDac();
				DataTable dt= dac.GetDataTable();
				this.ddlUID.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlUID.Items.Add(new ListItem(dt.Rows[i]["Phone"].ToString(),dt.Rows[i]["ID"].ToString()));
				}
		}
		/// <summary>
		///绑定产品名称
		/// </summary>
		public void bindPID()
		{
				ProductDac dac = new ProductDac();
				DataTable dt= dac.GetDataTable();
				this.ddlPID.Items.Add(new ListItem("请选择",""));
				for(int i=0;i<dt.Rows.Count;i++)
				{
						this.ddlPID.Items.Add(new ListItem(dt.Rows[i]["ProductName"].ToString(),dt.Rows[i]["ID"].ToString()));
				}
		}
		
    }
}
